package com.walter.dynamic.thread.pool.autoconfigure;

import com.walter.dynamic.thread.pool.core.AdaptiveThreadPoolAdjuster;
import com.walter.dynamic.thread.pool.core.DynamicThreadPoolExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.AsyncTaskExecutor;

import java.util.concurrent.TimeUnit;

@Slf4j
public class DynamicThreadPoolAutoConfiguration {

    @Bean(value = "dynamicThreadPoolExecutor")
    public AsyncTaskExecutor dynamicThreadPoolExecutor() {
        DynamicThreadPoolExecutor executor = new DynamicThreadPoolExecutor(
                8,// 核心线程数
                64,// 最大线程数
                60,// 线程存活时间
                1000// 队列最大数量
                );
        // 创建自适应调整器
        AdaptiveThreadPoolAdjuster adjuster = new AdaptiveThreadPoolAdjuster(executor,2,64 );
        // 启动自适应调整
        adjuster.startAdjustment(5, TimeUnit.SECONDS);
        log.info("DynamicThreadPoolExecutor initialized");
        return executor;
    }
}